package com.egao.common.manage.college;

import cn.hutool.core.util.StrUtil;
import com.egao.common.core.annotation.ApiPageParam;
import com.egao.common.core.annotation.OperLog;
import com.egao.common.core.exception.BusinessException;
import com.egao.common.core.web.BaseController;
import com.egao.common.core.web.JsonResult;
import com.egao.common.core.web.PageParam;
import com.egao.common.core.web.PageResult;
import com.egao.common.module.student.entity.StudentSignBlank;
import com.egao.common.module.student.entity.vo.StudentSignBlankListVO;
import com.egao.common.module.student.service.StudentSignBlankService;
import com.egao.common.module.system.constants.UserConstants;
import com.egao.common.module.system.entity.User;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;

/**
 * 学院空白协议管理
 * @author lym
 */
@Api(tags = "学院空白协议管理")
@RestController
@RequestMapping("${api}")
public class CollegeSignBlankController extends BaseController {

    @Autowired
    private StudentSignBlankService studentSignBlankService;

    private final String title = "学院空白协议管理";

    @PreAuthorize("hasAuthority('college:blank:list')")
    @OperLog(value = title, desc = "分页查询空白协议")
    @ApiOperation(value = "分页查询空白协议")
    @ApiPageParam
    @GetMapping("/college/blank/page")
    public PageResult<StudentSignBlankListVO> page(HttpServletRequest request) {
        User loginUser = getLoginUser();

        if (StrUtil.isBlank(loginUser.getCollegeCode())) {
            throw new BusinessException("没有找到您的学院信息，请联系就业中心");
        }

        PageParam<StudentSignBlankListVO> pageParam = new PageParam<>(request);

        pageParam.put("collegeCode", loginUser.getCollegeCode());

        String degree = null;
        degree = UserConstants.DEGREE_SCOPE_ALL.equals(loginUser.getDegreeScope()) ? null : loginUser.getDegreeScope();
        pageParam.put("degree", degree);

        if (UserConstants.STUDENT_SCOPE_PART.equals(loginUser.getStudentScope())) {
            pageParam.put("collegeUserId", loginUser.getUserId());
        }

        return studentSignBlankService.listPage(pageParam);
    }

    @PreAuthorize("hasAuthority('college:blank:detail')")
    @OperLog(value = title, desc = "查询详情")
    @ApiOperation("查询详情")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "id", value = "主键ID", required = true, dataType = "Integer")
    })
    @GetMapping("/college/blank/detail")
    public JsonResult detail(Integer id) {
        return studentSignBlankService.detail(id, getLoginUser().getUserId(), getLoginUser().getCollegeCode());
    }

    @PreAuthorize("hasAuthority('college:blank:update')")
    @OperLog(value = title, desc = "提交", result = true)
    @ApiOperation(value = "提交")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "studentSignBlank", value = "studentSignBlank", required = true, dataType = "studentSignBlank")
    })
    @PostMapping("/college/blank/submit")
    public JsonResult submit(@RequestBody StudentSignBlank studentSignBlank) {
        if (null == studentSignBlank.getId()) {
            return studentSignBlankService.add(studentSignBlank, getLoginUserId());
        }
        return studentSignBlankService.edit(studentSignBlank, getLoginUserId());
    }

    @PreAuthorize("hasAuthority('college:blank:detail')")
    @OperLog(value = title, desc = "空白协议打印表")
    @ApiPageParam
    @PostMapping("/college/blank/print")
    public JsonResult print(@RequestBody StudentSignBlank studentSignBlank) {
        return studentSignBlankService.print(studentSignBlank.getId(), null);
    }
}
